package com.erlin.common.validator.validators;

import com.erlin.common.validator.constraints.Ranges;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Objects;

/**
 * @date 2021/5/31 9:57 下午
 * @Author alin
 */
public class RangesValidator implements ConstraintValidator<Ranges,Integer> {
    private Ranges ranges;

    @Override
    public void initialize(Ranges constraintAnnotation) {
        this.ranges = constraintAnnotation;
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(Objects.isNull(value)){
            return false;
        }
        var ranges = this.ranges.ranges();
        for (int range : ranges){
            if(range == value){
                return true;
            }
        }
        return false;
    }
}
